// pages/Earnings/Balance/Balance.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getUserAccount } from '../../../api/user';
import { getOrderTypeEnums , getOrderFormSum } from '../../../api/earning';
import { getChannel } from '../../../api/brand';
const regeneratorRuntime = require('../../../common/runtime');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    hideBalance: false, // 是否隐藏余额
    balance: 0, // 用户余额
    recycleAmountTotal: 0, // 回收金额
    redPacketBalance: 0, // 红包收益
    navList: [
      // { id: 0, name: '去提现', path: '/pages/Earnings/WithdrawWechat/WithdrawWechat', icon: assetsAddress + '/earnings/withdrawIcon.png'},
      { id: 0, name: '去充值', path: '/pages/Earnings/UpTopBalance/UpTopBalance', icon: assetsAddress + '/earnings/withdrawIcon.png'},
      { id: 1, name: '收益明细', path: '/pages/Earnings/Transaction/Transaction', icon: assetsAddress + '/earnings/incomeIcon.png',noBorder: true},
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._getUserAccount();
    this._getOrderTypeEnums();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this._getUserAccount();
    setTimeout(() => {
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res, '刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 1;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 切换余额展示隐藏
  selHideShowBalance(){
    this.setData({
      hideBalance: !this.data.hideBalance,
    })
  },
  // 页面跳转
  async goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // console.log(JSON.stringify(item))
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        if(type == '去提现'){
          var hasWechat = await this._getChannel();
          console.log(hasWechat,'hasWechat')
          if(hasWechat == 1){
            wx.navigateTo({
              url: '/pages/Earnings/WithdrawWechat/WithdrawWechat',
            })
          }else{
            wx.navigateTo({
              url: `${url}`,
            })
          }
        }else{
          wx.navigateTo({
            url: `${url}`,
          })
        }
      }
    }
  },
  // 获取用户余额信息
  _getUserAccount(){
    let _this = this;
    getUserAccount({})
    .then(res => {
      if(res.success){
        _this.setData({
          balance: res.data.balance,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取交易订单类型
  _getOrderTypeEnums(){
    let _this = this;
    getOrderTypeEnums()
    .then(res => {
      if(res.success){
        // _this.setData({
        //   orderTypeList: res.data,
        // })
        for (let index = 0; index < res.data.length; index++) {
          const element = res.data[index];
          if(element.description == '回收卖家收款' || element.description == '任务奖励'){
            _this._getOrderFormSum({inOrderType: element.orderType,description: element.description});
          }
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取回收金额统计
  _getOrderFormSum({inOrderType,description}){
    let _this = this;
    getOrderFormSum({accountType: '0',inOrderType: inOrderType})
    .then(res => {
      if(res.success){
        if(description == '回收卖家收款'){
          _this.setData({
            recycleAmountTotal: res.data.totalSum,
          })
        }
        if(description == '任务奖励'){
          _this.setData({
            redPacketBalance: res.data.totalSum,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取提现通道
  _getChannel(){
    let _this = this;
    return new Promise((resolve,reject) => {
      getChannel({})
      .then(res => {
        if(res.success){
          for (let index = 0; index < res.data.content.length; index++) {
            const item = res.data.content[index];
            if(item.channelTag == 'WECHAT_TRANSFER_PAY'){
              resolve(1);
              return;
            }
          }
          resolve(0);
        }
      })
    })
  },
})